import {Command} from '../constant';
import {IMainReducer} from '../types';
import {Action} from 'typings';
import produce from 'immer';
import * as immerUtil from '@/redux/immer-util';

const INITIAL_STATE: IMainReducer = {
  isReady: false,
  isLoading: false,
  showMode: false,
  List: [],
  type: null,
  industriesId: null,
  city: null,
  keyWords: '',
  page: 1,
  size: 10,
  totalPage: 0,
  filterData: [
    {
      name: '地区',
      type: 'area',
      children: [],
    },
    {
      name: '商圈',
      type: 'hierarchy',
      children: [],
    },
    {
      name: '离我最近',
      type: 'select',
      children: [
        {
          children: [
            {
              name: '离我最近',
              value: '离我最近',
              id: 1,
            },
            {
              name: '好评优先',
              value: '好评优先',
              id: 15,
            },
            {
              name: '人气优先',
              value: '人气优先',
              id: 19,
            },
          ],
        },
      ],
    },
  ],
  filterDropdownValue: [
    [9, 1], //地址
    [0, 0], //商圈
    [[0]], //类型
  ],
  cityId: 55, //城市信息 默认南京
  regionId: 830, //商圈信息 默认山西路/湖南路
  sortType: 1, //排序 默认离我最近
  cat0Id: null, //一级类目ID
  cat1Id: null, //二级类目ID
};

export default function main(state = INITIAL_STATE, action: Action): IMainReducer {
  const {type, payload} = action;

  return produce<IMainReducer>(state, (draftState) => {
    switch (type) {
      //通用改变数据
      case Command.commonChange:
        return immerUtil.commonChange(draftState, {...payload, key: 'main'});

      //初始化
      case Command.init:
        draftState.isReady = true;
        for (let propKey in payload.main) {
          draftState[propKey] = payload.main[propKey];
        }
        return draftState;

      //重置
      case Command.clean:
        for (let propKey in INITIAL_STATE) {
          draftState[propKey] = INITIAL_STATE[propKey];
        }
        return draftState;
    }
  });
}
